﻿using Hongsh.Pay.Client;
using MapleLeaf.Pay.Trade;
using System;
using System.Globalization;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Media.Animation;


namespace MapleLeaf.Client{
    public class TradeStateConverter : IValueConverter {

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
            TradeState tradeState = (TradeState)value;
            object result;
            if (targetType == typeof(Brush)) {
                result = tradeState == TradeState.成功
                    ? SuccessColor
                    : tradeState == TradeState.失败 || tradeState == TradeState.关闭 ? FailedColor : UnknowColor;
            }
            else if (tradeState == TradeState.成功) {
                result = SuccessIcon;
            }
            else if (tradeState == TradeState.失败 || tradeState == TradeState.关闭) {
                result = FailedIcon;
            }
            else {
                if (UnknowStoryboard == null) {
                    UnknowStoryboard = UnknowIcon.Spin(0.0, 360.0, 2000, 0, null, 0.5, 0.5);
                    UnknowStoryboard.Begin();
                }
                result = UnknowIcon;
            }
            return result;
        }


        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
            throw new NotImplementedException();
        }


        public TradeStateConverter() {
           
        }

        static TradeStateConverter() {
            Default = new TradeStateConverter();
            UnknowStoryboard = null;
            UnknowColor = new SolidColorBrush(Color.FromRgb(251, 140, 0));
            SuccessColor = new SolidColorBrush(Color.FromRgb(67, 160, 71));
            FailedColor = new SolidColorBrush(Color.FromRgb(229, 57, 53));
            UnknowIcon = new TextBlock {
                Text = ''.ToString(),
                Foreground = UnknowColor
            };
            SuccessIcon = new TextBlock {
                Text = ''.ToString(),
                Foreground = SuccessColor
            };
            FailedIcon = new TextBlock {
                Text = ''.ToString(),
                Foreground = FailedColor
            };
        }


        public static TradeStateConverter Default;


        private static Storyboard UnknowStoryboard;


        private static readonly Brush UnknowColor;


        private static readonly Brush SuccessColor;


        private static readonly Brush FailedColor;

        private static readonly TextBlock UnknowIcon;


        private static readonly TextBlock SuccessIcon;


        private static readonly TextBlock FailedIcon;
    }
}
